package zzzhc.image.model;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CaptureTableModel extends NormalTableModel {

	private static final long serialVersionUID = 5332923689237093143L;

	private static String[] COLUMNS = { "序号", "采集图片", "对应文字" };

	public CaptureTableModel() {
		init(COLUMNS, new Class[] { String.class, BufferedImage.class,
				String.class }, new Boolean[] { false, false, true });
	}

	public void addImage(BufferedImage image) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(COLUMNS[1], image);
		addRow(map);
	}

	public void addColumn(BufferedImage image, String code) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(COLUMNS[1], image);
		map.put(COLUMNS[2], code);
		addRow(map);
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return String.valueOf(rowIndex);
		} else {
			return super.getValueAt(rowIndex, columnIndex);
		}
	}

	public BufferedImage getImage(int index) {
		return (BufferedImage) getValueAt(index, 1);
	}

	public String getCode(int index) {
		return (String) getValueAt(index, 2);
	}

	public List<BufferedImage> getImages() {
		int count = getRowCount();
		List<BufferedImage> result = new ArrayList<BufferedImage>();
		for (int i = 0; i < count; i++) {
			BufferedImage image = (BufferedImage) getValueAt(i, 1);
			result.add(image);
		}
		return result;
	}
}
